<template>
  <div class="elders-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h2>社区老人管理</h2>
        <p class="header-subtitle">管理和查看社区内所有老人的基本信息</p>
      </div>
      <button class="add-btn" @click="showAddDialog">
        <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
          <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
        </svg>
        添加老人
      </button>
    </div>

    <!-- 主要内容 -->
    <div class="main-content">
      <div class="table-card">
        <div class="card-header">
          <div class="header-icon">
            <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
              <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
            </svg>
          </div>
          <h3>老人信息列表</h3>
        </div>

        <div class="card-content">
          <div v-if="loading" class="loading-state">
            <div class="spinner"></div>
            <p>正在加载老人信息...</p>
          </div>
          
          <div v-else-if="elderList.length === 0" class="empty-state">
            <svg viewBox="0 0 24 24" width="48" height="48" fill="currentColor">
              <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z"/>
            </svg>
            <p>暂无老人信息</p>
          </div>
          
          <div v-else>
            <div class="table-container">
              <table class="elders-table">
                <thead>
                  <tr>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>身份证号</th>
                    <th>居住地址</th>
                    <th>健康状况</th>
                    <th>状态</th>
                    <th>紧急联系人</th>
                    <th>紧急联系电话</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="elder in currentPageData" :key="elder.elderId">
                    <td>
                      <div class="elder-info">
                        <div class="elder-avatar">
                          <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                            <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                          </svg>
                        </div>
                        {{ elder.name }}
                      </div>
                    </td>
                    <td>{{ elder.gender }}</td>
                    <td>{{ elder.age }}</td>
                    <td>{{ maskIdCard(elder.idCard) }}</td>
                    <td>{{ elder.address }}</td>
                    <td>{{ elder.healthSummary }}</td>
                    <td>
                      <span :class="['status-tag', elder.status]">
                        {{ elder.status === 'normal' ? '正常' : elder.status === 'warning' ? '警告' : '危险' }}
                      </span>
                    </td>
                    <td>{{ elder.emergencyContacts }}</td>
                    <td>{{ elder.emergencyPhone }}</td>
                    <td>
                      <div class="action-buttons">
                        <button class="view-btn" @click="viewHealthData(elder)">
                          <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
                            <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>
                          </svg>
                          健康
                        </button>
                        <button class="edit-btn" @click="showEditDialog(elder)">
                          <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
                            <path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/>
                          </svg>
                          编辑
                        </button>
                        <button class="delete-btn" @click="handleDelete(elder.elderId)">
                          <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor">
                            <path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/>
                          </svg>
                          删除
                        </button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>

            <!-- 分页组件 -->
            <div class="pagination" v-if="elderList.length > pageSize">
              <button 
                class="pagination-btn"
                :disabled="currentPage === 1"
                @click="currentPage--"
              >
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12l4.59-4.59z"/>
                </svg>
              </button>
              <span class="page-info">
                第 {{ currentPage }} / {{ totalPages }} 页
              </span>
              <button 
                class="pagination-btn"
                :disabled="currentPage === totalPages"
                @click="currentPage++"
              >
                <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
                  <path d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"/>
                </svg>
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 添加/修改对话框 -->
  <div class="dialog-overlay" v-if="showDialog" @click="closeDialog">
    <div class="dialog" @click.stop>
      <div class="dialog-header">
        <h3>{{ isEdit ? '修改老人信息' : '添加老人' }}</h3>
        <button class="close-btn" @click="closeDialog">
          <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
            <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
          </svg>
        </button>
      </div>
      <form @submit.prevent="handleSubmit" class="dialog-form">
        <div class="form-grid">
          <div class="form-item">
            <label>姓名 *</label>
            <input v-model="formData.name" required placeholder="请输入姓名" />
          </div>
          <div class="form-item">
            <label>性别 *</label>
            <select v-model="formData.gender" required>
              <option value="男">男</option>
              <option value="女">女</option>
            </select>
          </div>
          <div class="form-item">
            <label>年龄 *</label>
            <input type="number" v-model="formData.age" required placeholder="请输入年龄" />
          </div>
          <div class="form-item">
            <label>身份证号 *</label>
            <input v-model="formData.idCard" required placeholder="请输入身份证号" />
          </div>
          <div class="form-item full-width">
            <label>居住地址 *</label>
            <input v-model="formData.address" required placeholder="请输入居住地址" />
          </div>
          <div class="form-item full-width">
            <label>健康状况 *</label>
            <textarea v-model="formData.healthSummary" required placeholder="请输入健康状况"></textarea>
          </div>
          <div class="form-item">
            <label>状态 *</label>
            <select v-model="formData.status" required>
              <option value="normal">正常</option>
              <option value="warning">警告</option>
              <option value="danger">危险</option>
            </select>
          </div>
          <div class="form-item">
            <label>紧急联系人 *</label>
            <input v-model="formData.emergencyContacts" required placeholder="请输入紧急联系人" />
          </div>
          <div class="form-item">
            <label>紧急联系电话 *</label>
            <input v-model="formData.emergencyPhone" required placeholder="请输入紧急联系电话" />
          </div>
        </div>
        <div class="dialog-footer">
          <button type="button" class="cancel-btn" @click="closeDialog">
            <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
              <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
            </svg>
            取消
          </button>
          <button type="submit" class="submit-btn">
            <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
              <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
            </svg>
            确定
          </button>
        </div>
      </form>
    </div>
  </div>
  
  <!-- 健康数据图表弹窗 -->
  <div class="dialog-overlay" v-if="showHealthChart" @click="closeHealthChart">
    <div class="dialog health-chart-dialog" @click.stop>
      <div class="dialog-header">
        <h3>{{ selectedElderName }} - 健康数据图表</h3>
        <button class="close-btn" @click="closeHealthChart">
          <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
            <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
          </svg>
        </button>
      </div>
      <div class="dialog-content">
        <div v-if="healthChartDataLoading" class="chart-loading">
          <div class="spinner"></div>
          <p>正在加载健康数据...</p>
        </div>
        <div v-else-if="elderHealthData.length === 0" class="chart-empty">
          <p>暂无健康数据</p>
        </div>
        <div v-else class="chart-container">
          <div class="chart-wrapper">
            <v-chart 
              class="chart" 
              :option="heartRateChartOption" 
              autoresize
            />
          </div>
          <div class="chart-wrapper">
            <v-chart 
              class="chart" 
              :option="bloodOxygenChartOption" 
              autoresize
            />
          </div>
          <div class="chart-wrapper">
            <v-chart 
              class="chart" 
              :option="bloodPressureChartOption" 
              autoresize
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import request from '../../utils/request'
import * as echarts from 'echarts'
import EChart from 'vue-echarts'

const router = useRouter()
const loading = ref(true)
const currentPage = ref(1)
const pageSize = 10
const showDialog = ref(false)
const isEdit = ref(false)
const showHealthChart = ref(false)
const healthChartDataLoading = ref(false)
const selectedElderId = ref(null)
const selectedElderName = ref('')
const elderHealthData = ref([])

const formData = ref({
  elderId: null,
  name: '',
  gender: '男',
  age: '',
  idCard: '',
  address: '',
  healthSummary: '',
  status: 'normal',
  emergencyContacts: '',
  emergencyPhone: ''
})

const elderList = ref([])

// 计算总页数
const totalPages = computed(() => {
  return Math.ceil(elderList.value.length / pageSize)
})

// 计算当前页数据
const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize
  const end = start + pageSize
  return elderList.value.slice(start, end)
})

// 获取老人列表数据
const getElderList = async () => {
  loading.value = true
  try {
    const res = await request.get('/community/elderList')
    if (res.code === 1) {
      elderList.value = res.data
    }
  } catch (error) {
    console.error('获取老人列表失败:', error)
  } finally {
    loading.value = false
  }
}
// 心率图表配置
const heartRateChartOption = computed(() => {
  if (elderHealthData.value.length === 0) return {}

  const dates = elderHealthData.value.map(item => item.createTime)
  const heartRates = elderHealthData.value.map(item => item.heartRate)

  return {
    title: {
      text: '心率变化趋势',
      left: 'center',
      textStyle: {
        color: '#2c3e50',
        fontWeight: 'bold'
      }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(255, 255, 255, 0.9)',
      borderColor: '#4facfe',
      borderWidth: 1,
      padding: [8, 16],
      textStyle: {
        color: '#2c3e50'
      }
    },
    xAxis: {
      type: 'category',
      data: dates,
      axisLabel: {
        color: '#6c757d',
        fontSize: 12
      },
      axisLine: {
        lineStyle: {
          color: '#e9ecef'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f8f9fa'
        }
      }
    },
    yAxis: {
      type: 'value',
      name: '心率 (bpm)',
      nameTextStyle: {
        color: '#2c3e50'
      },
      axisLabel: {
        color: '#6c757d',
        fontSize: 12
      },
      axisLine: {
        lineStyle: {
          color: '#e9ecef'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f8f9fa'
        }
      }
    },
    series: [{
      data: heartRates,
      type: 'line',
      smooth: true,
      lineStyle: {
        color: '#4facfe',
        width: 3
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(79, 172, 254, 0.3)' },
          { offset: 1, color: 'rgba(79, 172, 254, 0.1)' }
        ])
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值', lineStyle: { color: '#4facfe' } }
        ],
        label: {
          formatter: '平均值: {c}',
          position: 'end',
          color: '#4facfe'
        }
      },
      emphasis: {
        focus: 'series'
      }
    }],
    grid: {
      left: '10%',
      right: '10%',
      bottom: '15%'
    }
  }
})
// 血氧图表配置
const bloodOxygenChartOption = computed(() => {
  if (elderHealthData.value.length === 0) return {}

  const dates = elderHealthData.value.map(item => item.createTime)
  const bloodOxygens = elderHealthData.value.map(item => item.bloodOxygen)

  return {
    title: {
      text: '血氧变化趋势',
      left: 'center',
      textStyle: {
        color: '#2c3e50',
        fontWeight: 'bold'
      }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(255, 255, 255, 0.9)',
      borderColor: '#28a745',
      borderWidth: 1,
      padding: [8, 16],
      textStyle: {
        color: '#2c3e50'
      }
    },
    xAxis: {
      type: 'category',
      data: dates,
      axisLabel: {
        color: '#6c757d',
        fontSize: 12
      },
      axisLine: {
        lineStyle: {
          color: '#e9ecef'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f8f9fa'
        }
      }
    },
    yAxis: {
      type: 'value',
      name: '血氧 (%)',
      nameTextStyle: {
        color: '#2c3e50'
      },
      axisLabel: {
        color: '#6c757d',
        fontSize: 12
      },
      axisLine: {
        lineStyle: {
          color: '#e9ecef'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f8f9fa'
        }
      }
    },
    series: [{
      data: bloodOxygens,
      type: 'line',
      smooth: true,
      lineStyle: {
        color: '#28a745',
        width: 3
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(40, 167, 69, 0.3)' },
          { offset: 1, color: 'rgba(40, 167, 69, 0.1)' }
        ])
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值', lineStyle: { color: '#28a745' } }
        ],
        label: {
          formatter: '平均值: {c}',
          position: 'end',
          color: '#28a745'
        }
      },
      emphasis: {
        focus: 'series'
      }
    }],
    grid: {
      left: '10%',
      right: '10%',
      bottom: '15%'
    }
  }
})
// 血压图表配置
const bloodPressureChartOption = computed(() => {
  if (elderHealthData.value.length === 0) return {}

  const dates = elderHealthData.value.map(item => item.createTime)
  const bloodPressures = elderHealthData.value.map(item => {
    // 血压数据是整型，代表收缩压
    return typeof item.bloodPressure === 'number' ? item.bloodPressure : 0
  })

  return {
    title: {
      text: '血压变化趋势',
      left: 'center',
      textStyle: {
        color: '#2c3e50',
        fontWeight: 'bold'
      }
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(255, 255, 255, 0.9)',
      borderColor: '#dc3545',
      borderWidth: 1,
      padding: [8, 16],
      textStyle: {
        color: '#2c3e50'
      }
    },
    xAxis: {
      type: 'category',
      data: dates,
      axisLabel: {
        color: '#6c757d',
        fontSize: 12
      },
      axisLine: {
        lineStyle: {
          color: '#e9ecef'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f8f9fa'
        }
      }
    },
    yAxis: {
      type: 'value',
      name: '血压 (mmHg)',
      nameTextStyle: {
        color: '#2c3e50'
      },
      axisLabel: {
        color: '#6c757d',
        fontSize: 12
      },
      axisLine: {
        lineStyle: {
          color: '#e9ecef'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f8f9fa'
        }
      }
    },
    series: [{
      data: bloodPressures,
      type: 'line',
      smooth: true,
      lineStyle: {
        color: '#dc3545',
        width: 3
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(220, 53, 69, 0.3)' },
          { offset: 1, color: 'rgba(220, 53, 69, 0.1)' }
        ])
      },
      markLine: {
        data: [
          { type: 'average', name: '平均值', lineStyle: { color: '#dc3545' } }
        ],
        label: {
          formatter: '平均值: {c}',
          position: 'end',
          color: '#dc3545'
        }
      },
      emphasis: {
        focus: 'series'
      }
    }],
    grid: {
      left: '10%',
      right: '10%',
      bottom: '15%'
    }
  }
})

// 查看健康数据
const viewHealthData = async (elder) => {
  selectedElderId.value = elder.elderId
  selectedElderName.value = elder.name
  showHealthChart.value = true
  healthChartDataLoading.value = true

  try {
    const res = await request.get('/community/healthData')
    if (res.code === 1) {
      // 筛选出当前老人的健康数据
      elderHealthData.value = res.data.filter(item => item.elderName === elder.name)
    }
  } catch (error) {
    console.error('获取健康数据失败:', error)
  } finally {
    healthChartDataLoading.value = false
  }
}

// 关闭健康图表弹窗
const closeHealthChart = () => {
  showHealthChart.value = false
  elderHealthData.value = []
  selectedElderId.value = null
  selectedElderName.value = ''
}

// 显示添加对话框
const showAddDialog = () => {
  isEdit.value = false
  const getCommunityId = () => {
    // 优先从localStorage获取社区ID
    const storedCommunityId = localStorage.getItem('communityId');
    if (storedCommunityId) {
      return storedCommunityId;
    }
    
    // 如果localStorage中没有，则从URL参数获取
    const urlParams = new URLSearchParams(window.location.search);
    return urlParams.get('communityId') || 'general'; // 默认为'general'
  }
  formData.value = {
    elderId: null,
    name: '',
    gender: '男',
    age: '',
    idCard: '',
    address: '',
    healthSummary: '',
    status: 'normal',
    emergencyContacts: '',
    emergencyPhone: '',
    communityId: getCommunityId()
  }
  showDialog.value = true
}

// 显示修改对话框
const showEditDialog = (elder) => {
  isEdit.value = true
  formData.value = { ...elder }
  showDialog.value = true
}

// 关闭对话框
const closeDialog = () => {
  showDialog.value = false
}

// 提交表单
const handleSubmit = async () => {
  try {
    if (isEdit.value) {
      await request.put('/community/updateElder', formData.value)
    } else {
      await request.post('/community/addElder', formData.value)
    }
    // 刷新列表
    await getElderList()
    closeDialog()
  } catch (error) {
    console.error('操作失败:', error)
  }
}

// 删除老人
const handleDelete = async (elderId) => {
  if (confirm('确定要删除该老人信息吗？')) {
    try {
      const id = elderId
      const res =  await request.delete(`/community/deleteElder/${id}`)
      if (res.code === 1) {
        alert('删除成功')
        await getElderList()
      }
    } catch (error) {
      console.error('删除失败:', error)
    }
  }
}

// 身份证号脱敏处理
const maskIdCard = (idCard) => {
  if (!idCard) return ''
  return idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1****$2')
}

onMounted(() => {
  getElderList()
})
</script>

<style scoped>
.elders-page {
  padding: 24px;
  background: linear-gradient(135deg, #f0f7ff 0%, #e6f2ff 100%);
  min-height: 100%;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  padding: 20px;
}

.page-header h2 {
  font-size: 24px;
  color: #2c3e50;
  margin: 0 0 8px 0;
  font-weight: 600;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}

.add-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
  box-shadow: 0 2px 6px rgba(79, 172, 254, 0.3);
}

.add-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 172, 254, 0.4);
}

.main-content {
  max-width: 1800px;
  margin: 0 auto;
}

.table-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  overflow: hidden;
}

.card-header {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  width: 24px;
  height: 24px;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.card-content {
  padding: 24px;
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #6c757d;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #e9ecef;
  border-top: 4px solid #4facfe;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-state p {
  margin: 0;
  font-size: 16px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #6c757d;
}

.empty-state svg {
  color: #ced4da;
  margin-bottom: 16px;
}

.empty-state p {
  margin: 0;
  font-size: 16px;
}

.table-container {
  overflow-x: auto;
}

.elders-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1000px;
}

.elders-table th,
.elders-table td {
  padding: 16px 12px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.elders-table th {
  background: #fafafa;
  font-weight: 600;
  color: #495057;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.elder-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.elder-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #e3f2fd;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4facfe;
}

.status-tag {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-tag.normal {
  background: #e8f7ef;
  color: #28a745;
}

.status-tag.warning {
  background: #fff3cd;
  color: #856404;
}

.status-tag.danger {
  background: #f8d7da;
  color: #721c24;
}

.action-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.view-btn, .edit-btn, .delete-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s ease;
  color: white;
  white-space: nowrap;
}

.view-btn {
  background: #17a2b8;
}

.view-btn:hover {
  background: #138496;
}

.edit-btn {
  background: #ffc107;
  color: #212529;
}

.edit-btn:hover {
  background: #e0a800;
}

.delete-btn {
  background: #dc3545;
}

.delete-btn:hover {
  background: #c82333;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px 0 0;
  gap: 16px;
}

.pagination-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid #dee2e6;
  background: #fff;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #495057;
}

.pagination-btn:hover:not(:disabled) {
  background: #4facfe;
  border-color: #4facfe;
  color: #fff;
}

.pagination-btn:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.page-info {
  color: #495057;
  font-size: 14px;
  font-weight: 500;
}

/* 对话框样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog {
  background: #fff;
  border-radius: 12px;
  width: 600px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
  font-weight: 500;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  color: #6c757d;
  transition: background 0.2s ease;
}

.close-btn:hover {
  background: #f1f3f4;
}

.dialog-form {
  padding: 24px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.form-item.full-width {
  grid-column: span 2;
}

.form-item label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-size: 14px;
  font-weight: 500;
}

.form-item input,
.form-item select,
.form-item textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #ced4da;
  border-radius: 6px;
  font-size: 14px;
  transition: all 0.3s;
  box-sizing: border-box;
}

.form-item input:focus,
.form-item select:focus,
.form-item textarea:focus {
  border-color: #4facfe;
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.form-item textarea {
  height: 100px;
  resize: vertical;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  border-top: 1px solid #eee;
}

.dialog-footer button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.cancel-btn {
  border: 1px solid #ced4da;
  background: #fff;
  color: #495057;
}

.cancel-btn:hover {
  background: #f8f9fa;
  border-color: #adb5bd;
}

.submit-btn {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  border: none;
  box-shadow: 0 2px 6px rgba(79, 172, 254, 0.3);
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(79, 172, 254, 0.4);
}

.health-chart-dialog {
  width: 90%;
  max-width: 1200px;
  height: 80vh;
}

.dialog-content {
  padding: 20px;
  height: calc(100% - 60px);
  display: flex;
  flex-direction: column;
}

.chart-loading, .chart-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #6c757d;
}

.chart-loading .spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #e9ecef;
  border-top: 4px solid #4facfe;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

.chart-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: 100%;
}

.chart-wrapper {
  flex: 1;
  min-height: 0;
}

.chart {
  width: 100%;
  height: 100%;
  min-height: 200px;
}

@media (max-width: 768px) {
  .elders-page {
    padding: 16px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .form-grid {
    grid-template-columns: 1fr;
  }
  
  .form-item.full-width {
    grid-column: span 1;
  }
  
  .elders-table th,
  .elders-table td {
    padding: 12px 8px;
    font-size: 13px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .dialog {
    width: 95vw;
  }
  
  .health-chart-dialog {
    width: 95%;
    height: 85vh;
  }
  
  .chart-container {
    flex-direction: column;
  }
}
</style>